package cn.hhz._05weibo.controller;

import cn.hhz._05weibo.common.response.JsonResult;
import cn.hhz._05weibo.common.response.StatusCode;
import cn.hhz._05weibo.mapper.CommentMapper;
import cn.hhz._05weibo.pojo.dto.CommentDTO;
import cn.hhz._05weibo.pojo.entity.Comment;
import cn.hhz._05weibo.pojo.vo.CommentVO;
import cn.hhz._05weibo.pojo.vo.UserV0;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论模块")
@RestController
@RequestMapping("v1/comment/")
public class CommentController {

    @Autowired
    CommentMapper commentMapper;
    @ApiOperation("微博评论功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        UserV0 userV0 = (UserV0) session.getAttribute("user");
        if(userV0==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        System.out.println(commentDTO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUser_id(userV0.getId());
        System.out.println(comment);
        commentMapper.insert(comment);
        return JsonResult.ok();
    }

    @ApiOperation("获取微博评论列表功能")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "int")
    public JsonResult selectByWeiboId(int id){
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVOS);

    }
}
